﻿using System;
using System.IO;
using Microsoft.Reporting.WebForms;
using Monkey.Warrenty.Biz;
using Monkey.Warrenty.Constants;
using MonkeyWarranty.Web.Common;

namespace MonkeyWarranty.Web.WarrantyTickets
{
    public partial class PrintTicket : System.Web.UI.Page
    {
        #region Properties

        private WarrantyTicketBiz _warrantyTicketBiz;
        private WarrantyTicketBiz WarrantyTicketManager
        {
            get { return _warrantyTicketBiz ?? (_warrantyTicketBiz = new WarrantyTicketBiz()); }
        }

        private long _ticketId;
        private long TicketId
        {
            get
            {
                if (_ticketId <= 0)
                    long.TryParse(Request.Params["id"], out _ticketId);
                return _ticketId;
            }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (WarrantyCommon.CurrrentEmployee == null)
            {
                Response.Redirect("~/Logon.aspx");
                return;
            }

            if (!WarrantyCommon.CurrrentEmployee.HasRole(RoleConstant.AdminBaoHanh))
            {
                Response.Redirect("~/NoPermission.aspx");
            }

            if (!Page.IsPostBack)
            {
                if (TicketId <= 0) return;

                FileStream report = null;
                FileStream subreport1 = null;
                FileStream subreport2 = null;

                try
                {
                    rvWarrantyTicket.ProcessingMode = ProcessingMode.Local;

                    // Load the report from a stream. 
                    report = new FileStream(Server.MapPath("~/Rdlcs/WarrantyTicket.rdlc"), FileMode.Open, FileAccess.Read);
                    rvWarrantyTicket.LocalReport.LoadReportDefinition(report);

                    //// Load all the subreports contained in the report using LoadSubReportDefinition. The name of the subreport must match the one defined
                    //// in the main report stream (in the <Subreport>\<ReportName> XML element)
                    subreport1 = new FileStream(Server.MapPath("~/Rdlcs/WarrantyTicketProducts.rdlc"), FileMode.Open, FileAccess.Read);
                    rvWarrantyTicket.LocalReport.LoadSubreportDefinition("WarrantyTicketProducts", subreport1);

                    subreport2 = new FileStream(Server.MapPath("~/Rdlcs/WarrantyTicketLendProducts.rdlc"), FileMode.Open, FileAccess.Read);
                    rvWarrantyTicket.LocalReport.LoadSubreportDefinition("WarrantyTicketLendProducts", subreport2);

                    var warrantyTicketForPrint = WarrantyTicketManager.GetWarrantyTicketForPrint(TicketId);
                    if (warrantyTicketForPrint != null)
                    {
                        // report data source
                        rvWarrantyTicket.LocalReport.DataSources.Add(
                            //new ReportDataSource("WarrantyTicketDataset", warrantyTicketForPrint));
                            new ReportDataSource(rvWarrantyTicket.LocalReport.GetDataSourceNames()[0], warrantyTicketForPrint));
                        // report data source "InstantTicketDetailsDataset"
                        //rvWarrantyTicket.LocalReport.DataSources.Add(
                        //    new ReportDataSource(rvWarrantyTicket.LocalReport.GetDataSourceNames()[1],
                        //                         warrantyTicketForPrint));

                        rvWarrantyTicket.LocalReport.SubreportProcessing += LocalReport_SubreportProcessing;
                    }
                }
                finally
                {
                    if (report != null)
                        report.Dispose();
                    if (subreport1 != null)
                        subreport1.Dispose();
                    if (subreport2 != null)
                        subreport2.Dispose();
                }
            }
        }

        private void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
        {
            switch (e.ReportPath)
            {
                case "WarrantyTicketProducts":
                    var ticketProductDetails = WarrantyTicketManager.GetWarrantyTicketProductsForPrint(TicketId);
                    if (ticketProductDetails != null)
                        e.DataSources.Add(new ReportDataSource(e.DataSourceNames[0], ticketProductDetails));
                    break;
                case "WarrantyTicketLendProducts":
                    var ticketLendProductDetails = WarrantyTicketManager.GetWarrantyTicketLendProductsForPrint(TicketId);
                    if (ticketLendProductDetails != null)
                        e.DataSources.Add(new ReportDataSource(e.DataSourceNames[0], ticketLendProductDetails));
                    break;
            }
        }
    }
}